<?php
namespace App\Process;

use App\Model\ShopsModel;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisObject;
use App\Utility\Pool\RedisPool;
use App\Utility\RedisClient;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Logger;
use EasySwoole\EasySwoole\ServerManager;

class SendMessage extends AbstractProcess{
    private $isRun = false;
    public function run($arg){
        //定时500ms检测有没有任务，有的话就while死循环执行
        $this->addTick(5000,function (){
            if(!$this->isRun){
                $this->isRun = true;
                go(function (){
                    MysqlPool::invoke(function (MysqlObject $db){
                        $model  = new ShopsModel($db);
                        $shopInfo = $model->select('fd')->where('mac','3c:71:bf:2a:38:9e')->first();
                        if($shopInfo){
                            $info = ServerManager::getInstance()->getSwooleServer()->connection_info($shopInfo['fd']);
                            if(is_array($info)){
                                ServerManager::getInstance()->getSwooleServer()->send($shopInfo['fd'],json_encode(['name'=>'001#']));
                            }
                        }
                    });
                    $this->isRun = false;
                });
            }
        });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}